//
//  ZJUserModel.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/26.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJUserModel.h"
//会员ID
NSString *allConstMD5id;

@interface ZJUserModel()
@property (nonatomic,strong) MMDrawerController * drawerController;

@end

@implementation ZJUserModel
+(void)loginWithPhone:(NSString *)phone password:(NSString *)password withReturnValeuBlock:(ReturnValueBlock)block withErrorCodeBlock:(ErrorCodeBlock)errorBlock withFailureBlock:(FailureBlock)failureBlock{
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    NSDictionary *parameter = @{
                                @"app_id":allConstApp_id,
                                @"mobile":phone,
                                @"code":password,
                                @"e_type":@"1",
                                @"e_width":@(SCREEN_WIDTH),
                                @"e_height":@(SCREEN_HEIGHT),
                                @"os_version":phoneVersion,
                                @"app_version":appVersion
                                };
    NSString *url = [LRNetwork requestURL:@"cgi/mobile_verify.php"];
    // 登录
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        allConstMemberid = returnValue[@"user_id"];
        allConstMD5id = [Utils md5:[NSString stringWithFormat:@"%@_%@|%@|%@",allConstApp_id,allConstProjectid,returnValue[@"user_id"],returnValue[@"verify"]]];
        [USER_DEFAULTS setObject:returnValue[@"user_id"] forKey:@"user_id"];
        [USER_DEFAULTS setObject:allConstMD5id forKey:@"verify"];
        [USER_DEFAULTS setObject:returnValue[@"verify"] forKey:@"verify1"];
        [USER_DEFAULTS synchronize];
        if (block) block(returnValue);
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        if (errorBlock) errorBlock(errorCode, errorMessage);
    } withFailureBlock:^{
        if (failureBlock) failureBlock();
    }];
}
+ (void)autoLogin
{
    appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    appBuild = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    NSDictionary *parameter = [LRNetwork requestParameter:@{
                                                            @"e_type":@"1",
                                                            @"e_width":@(SCREEN_WIDTH),
                                                            @"e_height":@(SCREEN_HEIGHT),
                                                            @"os_version":phoneVersion,
                                                            @"app_version":appVersion
                                                            }];
    NSString *url = [LRNetwork requestURL:@"cgi/open.php"];
    // 登录
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        allConstMemberid = returnValue[@"user_id"];
        allConstMD5id = [Utils md5:[NSString stringWithFormat:@"%@_%@|%@|%@",allConstApp_id,allConstProjectid,returnValue[@"user_id"],returnValue[@"verify"]]];
        [USER_DEFAULTS setObject:returnValue[@"user_id"] forKey:@"user_id"];
        [USER_DEFAULTS setObject:allConstMD5id forKey:@"verify"];
        [USER_DEFAULTS setObject:returnValue[@"verify"] forKey:@"verify1"];
        [USER_DEFAULTS synchronize];
        
        NSLog(@"用户数据更新成功");
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        
        NSString *key = @"CFBundleVersion";
        // 上一次的使用版本（存储在沙盒中的版本号）
        NSString *lastVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];;
        // 当前软件的版本号（从Info.plist中获得）
        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
        
        if (![currentVersion isEqualToString:lastVersion]) { // 版本号相同：这次打开和上次打开的是同一个版本
            static dispatch_once_t justDoOnce;
            dispatch_once(&justDoOnce, ^{
                
                NSLog(@"首次登入");
                
            });
            
        }

       
    } withFailureBlock:^{
        
    }];
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count = 0;
    Ivar * ivars = class_copyIvarList([self class], &count);
    for (int i = 0 ; i < count; i++) {
        Ivar ivar = ivars[i];
        const char * name = ivar_getName(ivar);
        NSString * key = [NSString stringWithUTF8String:name];
        //归档
        [aCoder encodeObject:[self valueForKey:key] forKey:key];
    }
    NSLog(@"更新用户信息");
    free(ivars);
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        //解档
        unsigned int count = 0;
        Ivar * ivars = class_copyIvarList([self class], &count);
        for (int i = 0 ; i < count; i++) {
            Ivar ivar = ivars[i];
            const char * name = ivar_getName(ivar);
            NSString * key = [NSString stringWithUTF8String:name];
            //解档
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        NSLog(@"成功读取用户信息");
        free(ivars);
    }
    return self;
}

@end
